package tools;

import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码生成器测试类
 * 用于快速生成BCrypt密码哈希值
 * 
 * @author AI Assistant
 * @date 2024-01-01
 */
public class PasswordGeneratorTest {
    
    /**
     * BCrypt密码编码器
     */
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    
    /**
     * 测试生成密码哈希值
     */
    @Test
    public void generatePasswordHash() {
        // 要加密的密码列表
        String[] passwords = {
            "admin123",
            "test123", 
            "user123",
            "manager123",
            "guest123"
        };
        
        System.out.println("=== 密码哈希值生成 ===");
        
        for (String password : passwords) {
            String hash = passwordEncoder.encode(password);
            System.out.printf("密码: %-12s -> 哈希: %s%n", password, hash);
            
            // 验证生成的哈希值
            boolean matches = passwordEncoder.matches(password, hash);
            System.out.printf("验证: %s%n%n", matches ? "✅ 通过" : "❌ 失败");
        }
    }
    
    /**
     * 生成特定密码的哈希值
     */
    @Test
    public void generateSpecificPassword() {
        String password = "your_password_here"; // 在这里输入您要加密的密码
        
        String hash = passwordEncoder.encode(password);
        
        System.out.println("=== 密码加密结果 ===");
        System.out.println("明文密码: " + password);
        System.out.println("BCrypt哈希: " + hash);
        
        // 生成SQL更新语句
        System.out.println("\n=== SQL更新语句 ===");
        System.out.printf("UPDATE sys_user SET password = '%s' WHERE username = 'your_username';%n", hash);
    }
    
    /**
     * 验证现有密码哈希值
     */
    @Test
    public void verifyExistingHash() {
        String password = "admin123";
        String existingHash = "$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iKXISwrpy.jbdqNdyeiXs0B0/XDu";
        
        boolean matches = passwordEncoder.matches(password, existingHash);
        
        System.out.println("=== 密码验证 ===");
        System.out.println("明文密码: " + password);
        System.out.println("哈希值: " + existingHash);
        System.out.println("验证结果: " + (matches ? "✅ 匹配" : "❌ 不匹配"));
    }
}